package controller;

import com.rain.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import service.LoginService;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 用户名密码登录
     *
     * @param loginName
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     */
    @PostMapping("loginByAccount")
    public Result loginByAccount(@RequestParam("loginName") String loginName, @RequestParam("password") String password) throws NoSuchAlgorithmException {
        MessageDigest instance = MessageDigest.getInstance(password);
        String digestPassword = instance.toString();
        boolean flag = loginService.checkLogin(loginName, digestPassword);
        if (!flag) {
            return Result.error("请检查用户名或密码是否正确");
        }
        return Result.success();
    }

    @PostMapping("registerAccount")
    public Result registerAccount(@RequestParam("loginName") String loginName, @RequestParam("password") String password) {
        return Result.success();
    }
}
